define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    sendmsg_url: 'user/user/sendmsg',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            $(".btn-grouping").click(function () {
                // 获取选中的数据id
                var ids = Table.api.selectedids(table).join(",");
                // 获取接口url，并将数据id拼接到url中
                var url = $(this).attr('data-url') + '?ids=' + ids;
                if (!url) return false;
                // 获取弹窗标题
                var title = $(this).attr('data-title');
                // 打开窗口
                Fast.api.open(url, title, {

                    callback: function (value) {}
                })
            });


            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        // {field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},

                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},

                        {field: 'team_id', title: __('组团'),
                         operate: 'select',
                         formatter: function (value, row, index) {
                            // 直接返回 team_name 值，如果不存在则返回原始值
                            return row.team_name ? row.team_name : (value ? value : '');
                         },
                            searchList: $.getJSON("user/team/selectpage")

                        },



                        {field: 'top_name', title: __('团长'), operate: false},
                        {field: 'catering', title: __('餐饮'), operate: 'LIKE'},
                        {field: 'house', title: __('住宿'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        sendmsg: function () {

             Controller.api.bindevent();


        }
    };
    return Controller;
});
